﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Tzx.TUpdater.WinHelp
{
    /// <summary>
    /// 帮助向导窗体
    /// </summary>
    public partial class WizardFrm : BaseForm //Form,BaseForm
    {
        /// <summary>
        /// 向导
        /// </summary>
        public WizardFrm()
        {
            InitializeComponent();

            Load += new EventHandler(WizardFrm_Load);
            BtnCheckUpdaterNumber.Click += new EventHandler(BtnCheckUpdaterNumber_Click);
            BtnUpdater.Click += new EventHandler(BtnUpdater_Click);
            BtnServerConfig.Click += new EventHandler(BtnServerConfig_Click);
        }


        /// <summary>
        /// 加载时
        /// </summary>
        void WizardFrm_Load(object sender, EventArgs e)
        {
            Text = AppGlobal.AppNameText;

            TxtArgs.Text = GetExeCallArgsHelpText();

            //取消全选
            TxtArgs.SelectionStart = 0;
            TxtArgs.SelectionLength = 0;
        }

        /// <summary>
        /// 更新文件个数
        /// </summary>
        private void BtnCheckUpdaterNumber_Click(object sender, EventArgs e)
        {
            int updateCount = 0;

            string exePath = Application.ExecutablePath;
            FileInfo info = new FileInfo(exePath);
            if (!info.Exists)
            {
                updateCount = -1;
            }
            else
            {
                ProcessStartInfo info2 = new ProcessStartInfo();
                info2.FileName = info.FullName;
                if (info.Directory != null)
                {
                    info2.WorkingDirectory = info.Directory.FullName;
                }
                info2.Arguments = string.Format("-{0}", Program.Args_checkforupdates);
                Process process = new Process();
                process.StartInfo = info2;
                process.Start(); //重新调用:获得更新文件个数
                //if (!waitForExit)
                //{
                //    return 0;
                //}
                process.WaitForExit();
                updateCount = process.ExitCode;
                process.Close();
            }

            //完成
            MessageBox.Show(string.Format("待更新文件总数：{0} 个。", updateCount), AppGlobal.AppNameText, MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }

        /// <summary>
        /// 服务端配置
        /// </summary>
        void BtnServerConfig_Click(object sender, EventArgs e)
        {
            DialogResult dlgR = MessageBox.Show("关闭本窗体，并运行服务端配置界面？", Text, MessageBoxButtons.YesNo,
                                                MessageBoxIcon.Question);
            if (dlgR == DialogResult.Yes)
            {
                Close();

                Process.Start(Application.ExecutablePath, string.Format("-{0}", Program.Args_gen)); //重新调用:运行服务端配置界面
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        void BtnUpdater_Click(object sender, EventArgs e)
        {
            DialogResult dlgR = MessageBox.Show("关闭本窗体，并运行运行？", Text, MessageBoxButtons.YesNo,
                                                MessageBoxIcon.Question);
            if (dlgR == DialogResult.Yes)
            {
                Close();

                Process.Start(Application.ExecutablePath, string.Format("-{0}", Program.Args_run)); //重新调用:运行更新界面
            }
        }

        /// <summary>
        /// 帮助信息
        /// </summary>
        /// <returns></returns>
        private static string GetExeCallArgsHelpText()
        {
            //"可不是这么随便点点敲敲就可以更新的哟,参考使用帮助:"

            StringBuilder sbHelp = new StringBuilder();
            sbHelp.AppendLine("客户应用程序调用自动更新,请使用具体参数:");
            sbHelp.AppendLine();

            sbHelp.AppendLine(string.Format("{0}", AppGlobal.ExeNameAndExt));

            sbHelp.AppendLine(string.Format("[/{0}],检查更新", Program.Args_checkforupdates));
            sbHelp.AppendLine();

            sbHelp.AppendLine(string.Format("[/{0}],执行更新", Program.Args_run));
            sbHelp.AppendLine();

            sbHelp.AppendLine(string.Format("[/{0}],检查若存存更新则执行更新", Program.Args_checkrun));
            sbHelp.AppendLine();

            sbHelp.AppendLine(
                string.Format("[/{0}:<{0}>],更新下载xml配置文件的url地址,如 http://xxx/updater/ 或 http://xxx/updater/{1}",
                    Program.Args_url,
                    AppGlobal.ExeName + ".xml"
                    ));
            sbHelp.AppendLine();

            sbHelp.AppendLine(string.Format("[/{0}],打开服务端配置信息生成工具界面", Program.Args_gen));
            sbHelp.AppendLine();

            sbHelp.AppendLine(string.Format("[/{0}] 或 [/{1}],打开本帮助向导界面", Program.Args_help, Program.Args_help2));
            sbHelp.AppendLine();

            //
            return sbHelp.ToString();
        }

    }
}
